<?php

App::uses('AppModel', 'Model');
App::uses('Evaluation', 'Model');
App::uses('UserArea', 'Model');
App::uses('Area', 'Model');
App::uses('Moe', 'Model');
App::uses('User', 'Model');
App::uses('Test', 'Model');

class Attempt extends AppModel
{
    const STATUS_DISABLED = 0;
    const STATUS_ENABLED = 1;

    /**
     * Obtenemos los examenes de un usuario.
     * En caso de que un examen se haya presentado mas de una vez solo se muestra
     * el resultado mas reciente o la calificación mas alta. Los criterios son
     * secuneciales.
     * @param string $userId UUID
     * @return array
     */
    public function getTestsByUserId($userId) {
        $filters = array(
            'fields' => array(
                'Attempt.id',
                'Attempt.user_id',
                'Attempt.test_id',
                'Attempt.calification',
                'Attempt.application_date',
            ),
            'joins' => array(
                array(
                    'type' => 'LEFT',
                    'table' => 'attempts',
                    'alias' => 'a2',
                    'conditions' => array(
                        'Attempt.user_id = a2.user_id',
                        'Attempt.test_id = a2.test_id',
                        'a2.status' => self::STATUS_ENABLED,
                        'a2.application_date > Attempt.application_date',
                    )
                ),
            ),
            'conditions' => array(
                'Attempt.user_id' => $userId,
                'Attempt.status' => self::STATUS_ENABLED,
                'a2.id IS NULL'
            ),
            'order' => array(
                'Attempt.application_date DESC'
            ),
        );
        $record = $this->flatArray($this->find('all', $filters));
        return $record;
    }

    /**
     * Obtenemos todos los registros para un usuario y un examen
     * @param string $userId UUID
     * @param string $testId UUID
     * @return array
     */
    public function getByUserIdAndTestId($userId, $testId)
    {
        $filters = array(
            'fields' => array(
                'Attempt.id',
                'Attempt.qualification',
                'Attempt.application_date',
            ),
            'joins' => array(
                array(
                    'type' => 'INNER',
                    'table' => 'evaluations',
                    'alias' => 'e',
                    'conditions' => array(
                        'e.user_id' => $userId,
                        'e.test_id' => $testId,
                        'e.status' => Evaluation::STATUS_ENABLED,
                    )
                ),
            ),
            'conditions' => array(
                'Attempt.evaluation_id = e.id',
                'Attempt.status' => self::STATUS_ENABLED,
            ),
            'order' => array(
                'Attempt.application_date DESC'
            ),
        );
        $record = $this->flatArray($this->find('all', $filters));
        return $record;
    }

    public function getCountAttempts($evaluationId)
    {
        $filters = array(
            'fields' => array(
                'COUNT(Attempt.id) as cant'
            ),
            'conditions' => array(
                'Attempt.evaluation_id' => $evaluationId,
                'Attempt.status' => self::STATUS_ENABLED,
            )
        );
        $record = $this->flatArray($this->find('all', $filters));
        return $record;
    }

    public function getEnabledByEvaluation($evaluationId)
    {
        $filters = array(
            'fields' => array(
                'Attempt.qualification as qualification',
                'Attempt.application_date as application_date',
                't.minimum_qualification as min_cal'
            ),
            'joins' => array(
                array(
                    'type' => 'INNER',
                    'table' => 'evaluations',
                    'alias' => 'e',
                    'conditions' => array(
                        'e.id' => $evaluationId,
                        'e.status' => Evaluation::STATUS_ENABLED,
                    )
                ),
                array(
                    'type' => 'INNER',
                    'table' => 'tests',
                    'alias' => 't',
                    'conditions' => array(
                        't.id = e.test_id',
                        'e.status' => Evaluation::STATUS_ENABLED,
                    )
                ),
            ),
            'conditions' => array(
                'Attempt.evaluation_id' => $evaluationId,
                'Attempt.status' => self::STATUS_ENABLED,
            ),
            'order' => array(
                'Attempt.application_date'
            ),
        );
        $record = $this->flatArray($this->find('all', $filters));
        return $record;
    }

    public function getEnabledUndated($userId)
    {
        $filters = array(
            'fields' => array(
                'Attempt.id as value',
                'u.nomina as nomina',
                'u.full_name as name',
                'Attempt.qualification as calif',
                'Attempt.application_date as date_att',
                't.name as nameTest'
            ),
            'joins' => array(
                array(
                    'type' => 'INNER',
                    'table' => 'evaluations',
                    'alias' => 'e',
                    'conditions' => array(
                        'e.id = Attempt.evaluation_id',
                        'e.status' => Evaluation::STATUS_ENABLED,
                    )
                ),
                array(
                    'type' => 'INNER',
                    'table' => 'tests',
                    'alias' => 't',
                    'conditions' => array(
                        't.id = e.test_id',
                        't.status' => Test::STATUS_ENABLED,
                    )
                ),
                array(
                    'type' => 'INNER',
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'conditions' => array(
                        'u.id = e.user_id',
                        'u.status' => User::STATUS_ENABLED,
                    )
                )
            ),
            'conditions' => array(
                'Attempt.application_date IS NULL',
                'Attempt.status' => self::STATUS_ENABLED,
            ),
            'order' => array(
                'u.nomina ASC',
                'u.full_name ASC',
                't.name',
            ),
        );
        $record = $this->flatArray($this->find('all', $filters));
        return $record;
    }

    public function getAttemptByUserTest($utId) {
        $filters = array(
            'fields' => array(
                'Attempt.*'
            ),
            'joins' => array(
                array(
                    'type' => 'INNER',
                    'table' => 'user_tests',
                    'alias' => 'ut',
                    'conditions' => array(
                        'ut.id' => $utId,
                        'ut.schedule_id = Attempt.schedule_id',
                        'ut.status' => UserTest::STATUS_ENABLED,
                    )
                ),
                array(
                    'type' => 'INNER',
                    'table' => 'evaluations',
                    'alias' => 'e',
                    'conditions' => array(
                        'e.id = Attempt.evaluation_id',
                        'e.user_id = ut.user_id',
                        'e.status' => Evaluation::STATUS_ENABLED,
                    )
                )
            ),
            'conditions' => array(
                'Attempt.status' => self::STATUS_ENABLED,
            )
        );
        $record = $this->find('first', $filters);
        return $record;
    }

    /**
     * Buscamos un intento por su schedule_id
     * @param $scheduleId UUID
     * @return array|null
     */
    public function getByScheduleAndUser($scheduleId, $userId)
    {
        $filters = array(
            'joins' => array(
                array(
                    'table' => 'evaluations',
                    'alias' => 'e',
                    'type' => 'inner',
                    'conditions' => array(
                        'e.id = Attempt.evaluation_id'
                    ),
                ),
            ),
            'conditions' => array(
                'schedule_id' => $scheduleId,
                'e.user_id' => $userId,
                'Attempt.status' => self::STATUS_ENABLED
            ),
            'limits' => 1,
        );
        return $this->find('first', $filters);
    }

    public function getLastAttempt($evaluationId) {
        $filters = array(
            'limit' => 1,
            'order' => array('application_date DESC'),
            'conditions' => array(
                'Attempt.evaluation_id' => $evaluationId,
                'Attempt.status' => self::STATUS_ENABLED,
            )
        );
        $record = $this->flatArray($this->find('all', $filters));
        return $record;
    }

    public function getAtteptsForKardex($userId) {
        $filters = array(
            'fields' => array(
                't.name as course',
                'IF(Attempt.qualification >= t.minimum_qualification, 1, 0) as aprobated',
                'Attempt.qualification as calif',
                'Attempt.application_date as date',
                't.id as test'
            ),
            'joins' => array(
                array(
                    'type' => 'INNER',
                    'table' => 'evaluations',
                    'alias' => 'e',
                    'conditions' => array(
                        'e.id = Attempt.evaluation_id',
                        'e.status' => Evaluation::STATUS_ENABLED,
                    )
                ),
                array(
                    'type' => 'INNER',
                    'table' => 'tests',
                    'alias' => 't',
                    'conditions' => array(
                        't.id = e.test_id',
                        't.status' => Test::STATUS_ENABLED,
                    )
                ),
                array(
                    'type' => 'LEFT',
                    'table' => 'attempts',
                    'alias' => 'a',
                    'conditions' => array(
                        'Attempt.evaluation_id = a.evaluation_id',
                        'Attempt.application_date < a.application_date',
                        'a.status' => self::STATUS_ENABLED,
                    )
                )
            ),
            'conditions' => array(
                'a.id IS NULL',
                'e.user_id' => $userId,
                'Attempt.status' => self::STATUS_ENABLED,
            ),
            'order' => array(
                'date ASC'
            ),
        );
        $record = $this->flatArray($this->find('all', $filters));
        return $record;
    }

}
